package org.uoa;

import java.io.IOException;
import java.io.StringReader;
import java.util.*;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;
import javax.servlet.http.*;
import javax.servlet.ServletException;

import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import org.uoa.*;

@SuppressWarnings("serial")
public class UbicsmartgridServlet extends HttpServlet {
	
	private static final PersistenceManagerFactory pmfInstance =
	        JDOHelper.getPersistenceManagerFactory("transactions-optional");
	 
	public static PersistenceManagerFactory get() {
	        return pmfInstance;
	}
	 
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		int count = 0;
		
		//resp.setContentType("text/plain");
		resp.setContentType("application/x-csv");
		resp.setHeader("Content-Disposition", "attachment;filename=" + "test.csv");
		 
		PersistenceManagerFactory pmfInstance = get();
		PersistenceManager pm = pmfInstance.getPersistenceManager();
		String query_str = "select from " + StoreData.class.getName();
		Query query =  pm.newQuery(query_str);
		query.setOrdering("date desc");
		List<StoreData> list = (List<StoreData>)query.execute();
		Hashtable hash = new Hashtable();
		for(StoreData d : list){
			Date date = d.getDate();
			if (hash.get(date.toString())==null) {
				resp.getWriter().println("date="+date.toString()+","+"power="+Integer.toString(new Integer(d.getPower())));
				count++;
				hash.put(date.toString(), d.getPower());
			}
				
			if (count == 24) {
		    	break;
		    }
		}
		pm.close();
	}
	
	public Calendar getLastDate(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		Calendar cal = null;
	
		PersistenceManagerFactory pmfInstance = get();
		PersistenceManager pm = pmfInstance.getPersistenceManager();
		String query_str = "select from " + StoreData.class.getName();
		resp.setContentType("text/plain");
		
		//resp.getWriter().println(query_str);
		Query query =  pm.newQuery(query_str);
		query.setOrdering("date desc");
		List<StoreData> list = (List<StoreData>)query.execute();
		for(StoreData d : list){
			Date date = d.getDate();
			cal = Calendar.getInstance();
		    cal.setTime( date );
			break;
		}
		pm.close();
		return cal;
	}
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException
	{		
		Calendar cal = null;
		try {
		String content = req.getParameter("content");
		cal = getLastDate(req, resp);
		if (cal != null) {
			Date tes = cal.getTime();
			resp.setContentType("text/plain");
			resp.getWriter().println(tes.toString());
		}
		SAXParserFactory factory = SAXParserFactory.newInstance();
		SAXParser parser = factory.newSAXParser();
		SGXMLHandler handler = new SGXMLHandler(req, resp, cal);
		parser.parse(new InputSource(new StringReader(content)) , handler);
		} catch(SAXException e) {
		} catch(ParserConfigurationException e){
		} finally {
			
		}
	}
}
